#coding=utf-8

import os
from openpyxl import load_workbook
from openpyxl.drawing.image import  Image

ExcelPath = os.getcwd() + "\\Gretag24P_Results_Templates.xlsx"

sheetForLightAndDetail = "Photo_Exposure_1.1"
sheetForDeadAndDeltaAB = "Photo_Color_3"

widthScale = 0.63
heightScale = 0.66

DeadLeaveRoiImgMap = {"20Lux_TL84":"C39", "100Lux_TL84":"F39", "300Lux_TL84":"I39", "1000Lux_TL84":"L39",
                      "5Lux_A": "C50", "20Lux_A":"F50", "100Lux_A":"I50", "1000Lux_D65":"L50"}

DeadLeaveDeltaABMap = {"20Lux_TL84":(38, 4), "100Lux_TL84":(38, 7), "300Lux_TL84":(38, 10), "1000Lux_TL84":(38, 13),
                      "5Lux_A": (49, 4), "20Lux_A":(49, 7), "100Lux_A":(49, 10), "1000Lux_D65":(49, 13)}

L_Map = {"1Lux_H": (69, 23), "5Lux_A":(70, 22), "20Lux_A":(71, 22), "20Lux_TL84":(71, 20), "100Lux_A":(72, 22), "100Lux_TL84":(72, 20), "300Lux_A":(73, 22), "300Lux_TL84":(73, 20), "1000Lux_D65":(74, 21), "1000Lux_TL84":(74, 20)}
DetailReserve_Map={"1Lux_H": (84, 23), "5Lux_A":(85, 22), "20Lux_A":(86, 22), "20Lux_TL84":(86, 20), "100Lux_A":(87, 22), "100Lux_TL84":(87, 20), "300Lux_A":(88, 22), "300Lux_TL84":(88, 20), "1000Lux_D65":(89, 21), "1000Lux_TL84":(89, 20)}

def ClearImgInExcel(excelPath, sheetName, cell):
    wb = load_workbook(excelPath)
    ws = wb.active
    sheet = wb[sheetName]
    sheet[cell] = ""
    # sheet.add_image(img, cell)
    wb.save(excelPath)
    wb.close()

def InsertImg2Excel(imgPath, excelPath, sheetName, cell):
    """
    存储图像到指定的cell;
    :param imgPath:
    :param excelPath:
    :param cell:
    :return:
    """
    wb = load_workbook(excelPath)
    ws = wb.active
    sheet = wb[sheetName]
    img = Image(imgPath)
    img.width = img.width * widthScale
    img.height = img.height * heightScale
    sheet[cell] = ""
    sheet.add_image(img, cell)
    wb.save(excelPath)
    wb.close()

def InsertTxt2Excell(msg, excelPath,sheetName, row, col):
    """
    插入文本到指定的单元格；
    :param msg:
    :param excelPath:
    :param sheetName:
    :param row:
    :param col:
    :return:
    """
    wb = load_workbook(excelPath)
    sheet = wb[sheetName]
    sheet.cell(row, col).value = msg
    wb.save(excelPath)

def GetPosition(key):
    """
    获取位置；
    :param key:
    :return:
    """
    pos = [None] * 4
    tmpAB_pos = DeadLeaveDeltaABMap.has_key(key)
    if tmpAB_pos:
        pos[0] = DeadLeaveDeltaABMap[key]

    tmpRoiImg_pos = DeadLeaveRoiImgMap.has_key(key)
    if tmpRoiImg_pos:
        pos[1] = DeadLeaveRoiImgMap[key]

    tmpL_pos = L_Map.has_key(key)
    if tmpL_pos:
        pos[2] = L_Map[key]

    tmpDR_pos = DetailReserve_Map.has_key(key)
    if tmpDR_pos:
        pos[3] = DetailReserve_Map[key]
    return pos

# ClearImgInExcel(ExcelPath,sheetForDeadAndDeltaAB, "C39")
# InsertImg2Excel("test.jpg", ExcelPath, sheetForDeadAndDeltaAB,"C39")
# InsertTxt2Excell("tttt", ExcelPath, sheetForDeadAndDeltaAB, 38, 4)
# InsertTxt2Excell("tttt", ExcelPath, sheetForDeadAndDeltaAB, 38, 7)
# InsertTxt2Excell("10", ExcelPath, sheetForDeadAndDeltaAB, 38, 10)
# InsertTxt2Excell("10", ExcelPath, sheetForDeadAndDeltaAB, 38, 13)